package com.yihuier.yihuier.Controller.UserManager;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yihuier.yihuier.Controller.Common.FileController;
import com.yihuier.yihuier.Controller.Common.TimeJudgeController;
import com.yihuier.yihuier.Controller.MeetingManager.MeetingRoomController;
import com.yihuier.yihuier.Entity.UserManager.Department;
import com.yihuier.yihuier.Entity.UserManager.User;
import com.yihuier.yihuier.Service.MeetingManager.MeetingCollectionService;
import com.yihuier.yihuier.Service.MeetingManager.MeetingService;
import com.yihuier.yihuier.Service.RoomManager.RoomService;
import com.yihuier.yihuier.Service.UserManager.*;
import com.yihuier.yihuier.Util.MailAssistant;
import com.yihuier.yihuier.Util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/UserManager/Users/")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DepartmentUserService departmentUserService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private MeetingCollectionService meetingCollectionService;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private UserFaceInfoService userFaceInfoService;

    @Autowired
    private MeetingService meetingService;

    @Autowired
    private RoomService roomService;

    @Autowired
    MeetingRoomController meetingRoomObj;

    @Autowired
    TimeJudgeController timeJudgeController;

    @GetMapping("users")
    public ResponseMessage findUserByUsername(@PathParam("username") String username, HttpServletRequest request) {
        //根据用户名查询到用户ID
        String user_id = userService.findUserByUserAccount(username);
        if (user_id == null) {
            //此时数据库中无此用户
            return new ResponseMessage(ResponseMessage.ERROR, "无此用户");
        } else {
            //此时数据库中有此用户
            //记录下id方便查询
            HttpSession session = request.getSession();
            session.setAttribute("user_id", user_id);
            return new ResponseMessage(ResponseMessage.SUCCESS, "有此用户");
        }
    }

    /**
     * 根据微信openid登陆
     */
    @PostMapping("wechatlogin.do")
    public ResponseMessage wechatlogin(@PathParam("openid") String openid, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = userService.loadUserByUserWechat(openid);
        if (user != null) {
            session.setAttribute("user_obj", user);
            HashMap<String, Integer> data = new HashMap<>();
            data.put("user_id", user.getUser_id());
            return new ResponseMessage(ResponseMessage.SUCCESS, "登录成功", data);
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "用户已被注销");
    }

    /**
     * 根据用户账号/用户邮箱/用户手机号登陆
     */
    @CrossOrigin(allowCredentials="true")
    @PostMapping("login.do")
    public ResponseMessage login(@PathParam("username") String username, @PathParam("password") String password, @PathParam("type") String type, @RequestParam(value = "openid", required = false, defaultValue = "默认错误openid") String openid, HttpServletRequest request) {
        HttpSession session = request.getSession();
        //获取预先存好的用户ID
        String user_id = null;
        if (session.getAttribute("user_id") == null) {
            //如果没有根据用户名查询到然后设置到session中
            if (type.equals("account")) {
                user_id = userService.findUserByUserAccount(username);
            } else if (type.equals("phone")) {
                user_id = userService.findUserByUserPhone(username);
            } else if (type.equals("email")) {
                user_id = userService.findUserByUserEmail(username);
            }
        }
        if (user_id == null) {
            String Md5EncodePassword = DigestUtils.md5DigestAsHex(password.getBytes());//md5加密密码
            String manager_id = managerService.getManagerIdByAccountAndPassword(username, Md5EncodePassword);//如果是“一会儿”公司的管理员
            if (manager_id != null) {
                String super_company_id = managerService.judgeSuperManager(manager_id);
                if (super_company_id.equals("1")) {//如果该用户是管理员并且是超级管理员
                    HashMap<String, Object> data = new HashMap<>();
                    data.put("super_manager_id", manager_id);
                    data.put("type", 3);
                    session.setAttribute("super_manager_id", manager_id);//设置session管理端管理员id信息
                    return new ResponseMessage(ResponseMessage.SUCCESS, "登录成功", data);
                }
                HashMap<String, Object> data = new HashMap<>();
                data.put("manager_id", manager_id);
                data.put("type", 1);
                session.setAttribute("manager_id", manager_id);//设置session管理端管理员id信息
//                System.out.println(session.getId());
                return new ResponseMessage(ResponseMessage.SUCCESS, "登录成功", data);
            }
            return new ResponseMessage(ResponseMessage.ERROR, "无此用户");
        } else {
            String Md5EncodePassword = DigestUtils.md5DigestAsHex(password.getBytes());//md5加密密码
            User user = userService.loadUserByUserAccount(user_id, Md5EncodePassword);
            if (user != null) {
                if (!openid.equals("默认错误openid")) {
                    userService.bingdingOpenidToUser(openid, user_id, Md5EncodePassword);//将用户openid绑定到用户
                }
                HashMap<String, Object> data = new HashMap<>();
                data.put("user_id", user_id);
                data.put("type", 2);
                return new ResponseMessage(ResponseMessage.SUCCESS, "登录成功", data);
            }
            return new ResponseMessage(ResponseMessage.ERROR, "密码错误");
        }
    }

    /**
     * 管理员/超级管理员退出时清空session
     * */
    @PostMapping("logout.do")
    public ResponseMessage logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return new ResponseMessage(ResponseMessage.SUCCESS, "登出成功");
    }


    @GetMapping("latestUsers")
    public ResponseMessage getLatestUsers(@RequestParam(value = "offset", defaultValue = "-1") int offset, @RequestParam(value = "rows", defaultValue = "-1") int rows) {
        List<User> results = userService.getLatestUsers(offset, rows);
        if (results != null) {
            return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", results);
        } else {
            return new ResponseMessage(ResponseMessage.ERROR, "数据库错误");
        }
    }

    /**
     * 获取用户部门编号
     */
    @PostMapping("getPersonalDepartmentId.json")
    public ResponseMessage getDepartmentIdByUserId(@PathParam("user_id") int user_id) {
        return new ResponseMessage(ResponseMessage.SUCCESS, "根据用户编号获取所在部门编号", departmentUserService.getDepartmentIdByUserId(user_id));
    }

    /**
     * 根据用户编号获取用户公司编号
     */
    @PostMapping("getPersonalCompanyId.json")
    public ResponseMessage getConpanyIdIdByDepartmentId(@PathParam("user_id") int user_id) {
        int department_id = departmentUserService.getDepartmentIdByUserId(user_id);
        return new ResponseMessage(ResponseMessage.SUCCESS, "根据用户编号获取所在公司编号", departmentService.getCompanyIdByDepartmentId(department_id));
    }

    /**
     * 根据用户编号获取所在公司的部门编号和部门名称
     */
    @PostMapping("getDepartmentIdsAndDepartmentNames.json")
    public ResponseMessage getDepartmentIdsAndDepartmentNames(@PathParam("user_id") int user_id) {
        int department_id = departmentUserService.getDepartmentIdByUserId(user_id);
        int company_id = departmentService.getCompanyIdByDepartmentId(department_id);
        ArrayList<Department> DepartmentIdsAndDepartmentNames = departmentService.getDepartmentIdsAndDepartmentNames(company_id);
        return new ResponseMessage(ResponseMessage.SUCCESS, "根据用户编号获取所在公司的部门编号和部门名称", DepartmentIdsAndDepartmentNames);
    }

    /**
     * 组装人员选择数据
     *
     * @param user_id 用户编号
     * @return 该用户所在部门名称，该部门所有用户的user_id,user_name,user_headpic_dir,user_email
     */
    @PostMapping("getPersonalCompanyUsers.json")
    public ResponseMessage getPersonlCompanyUsers(@PathParam("user_id") int user_id, HttpServletRequest request) {
        ArrayList<Map<Object, Object>> personalCompanyDepartmentUsers = new ArrayList<Map<Object, Object>>();
        int department_id = departmentUserService.getDepartmentIdByUserId(user_id);
        int company_id = departmentService.getCompanyIdByDepartmentId(department_id);
        ArrayList<Department> Departments = departmentService.getDepartmentIdsAndDepartmentNames(company_id);
        for (Department department : Departments) {
            ArrayList<HashMap<Object, Object>> userList = new ArrayList<HashMap<Object, Object>>();
            HashMap<Object, Object> maptight = new HashMap<Object, Object>();
            maptight.put("department_id", department.getDepartment_id());//1
            maptight.put("department_name", department.getDepartment_name());//财务部
            ArrayList<Integer> user_ids = departmentUserService.getUsersByDepartmentId(department.getDepartment_id());//获得所在部门的所有用户编号1，4
            for (int personal_user_id : user_ids) {
                HashMap<Object, Object> userMap = new HashMap<Object, Object>();
                if (personal_user_id == user_id) {
                    continue;
                }
                User specified_user = userService.getUserinfomationByUserid(personal_user_id);
                if (specified_user == null) {
                    continue;
                }
                userMap.put("user_id", specified_user.getUser_id());
                userMap.put("user_name", specified_user.getUser_name());
                userMap.put("user_headpic_dir", FileController.filePathDispose(specified_user.getUser_headpic_dir()));
                userMap.put("user_email", specified_user.getUser_email());
                userList.add(userMap);//该部门新添一个用户信息
            }//该部门所有用户添加完成
            maptight.put("users", userList);//将该部门的所有部件安装完成
            personalCompanyDepartmentUsers.add(maptight);//组装好所有信息
//            userList.clear();//每次清空userList，否则每次都会在此基础上增加
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "根据用户编号分组获取所在公司所有用户", personalCompanyDepartmentUsers);
    }

    /**
     * 获取用户头像url与用户名
     *
     * @param user_id 用户编号
     * @return 用户名与用户头像url
     */
    @PostMapping("userHeaderPic.json")
    public ResponseMessage getUserPicUrl(@PathParam("user_id") int user_id, HttpServletRequest request) {
        User user_info = userService.getUserinfomationByUserid(user_id);
        if (user_info != null) {
            HashMap<String, String> UsernameAndHeadPic = new HashMap<String, String>();
            UsernameAndHeadPic.put("user_name", user_info.getUser_name());
            UsernameAndHeadPic.put("user_headpic_dir", FileController.filePathDispose(user_info.getUser_headpic_dir()));
            return new ResponseMessage(ResponseMessage.SUCCESS, "用户头像与用户名获取成功", UsernameAndHeadPic);
        }
        return new ResponseMessage(ResponseMessage.ERROR, "无此用户或者用户已被注销");
    }

    /**
     * 修改密码
     */
    @PostMapping("updatePassword.do")
    public ResponseMessage updateUserPassword(@PathParam("user_id") int user_id, @PathParam("oldPassword") String oldPassword, @PathParam("newPassword") String newPassword, HttpServletRequest request) {
        String Md5EncodeOldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());//md5加密旧密码
        String Md5EncodeNewPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());//md5加密新密码
        int results = userService.updateUserPassword(user_id, Md5EncodeOldPassword, Md5EncodeNewPassword);
        if (results == 1) {
            return new ResponseMessage(ResponseMessage.SUCCESS, "密码更新成功");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "旧密码不正确");
    }

    /**
     * 根据密保问题重置密码
     */
    @PostMapping("updatePasswordBySecretProtection.do")
    public ResponseMessage updateUserPasswordBySecretProtection(@PathParam("user_id") int user_id, @PathParam("password") String password, HttpServletRequest request) {
        String Md5EncodeOldPassword = DigestUtils.md5DigestAsHex(password.getBytes());//md5加密密码
        int results = userService.updateUserPasswordBySecretProtection(user_id, Md5EncodeOldPassword);
        if (results == 1) {
            return new ResponseMessage(ResponseMessage.SUCCESS, "密码更新成功");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "密码更新失败");
    }

    /**
     * 获得密保问题及密码
     */
    @PostMapping("SecretProtectionProblems.json")
    public ResponseMessage getSecretProtectionProblemsByUserId(@PathParam("user_id") int user_id, HttpServletRequest request) {
        User userProtectionProblems = userService.getSecretProtectionProblems(user_id);
        LinkedHashMap<Object, Object> problemMap = new LinkedHashMap<Object, Object>();
        problemMap.put("question1", questionService.getquestion(userProtectionProblems.getAnswer_1_no()));
        problemMap.put("answer1", userProtectionProblems.getAnswer_1());
        problemMap.put("question2", questionService.getquestion(userProtectionProblems.getAnswer_2_no()));
        problemMap.put("answer2", userProtectionProblems.getAnswer_2());
        problemMap.put("question3", questionService.getquestion(userProtectionProblems.getAnswer_3_no()));
        problemMap.put("answer3", userProtectionProblems.getAnswer_3());
        return new ResponseMessage(ResponseMessage.SUCCESS, "密保问题及答案获取成功", problemMap);
    }

    /**
     * 获得密保问题
     */
    @PostMapping("AllProtectionProblems.json")
    public ResponseMessage getProtectionProblems(HttpServletRequest request) {
        List<Map<String, String>> problemsAndIds = questionService.getQuestions();
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取所有密保问题", problemsAndIds);
    }

    /**
     * 存储与我们联系的客户信息
     */
    @PostMapping("customerContactUs.do")
    public ResponseMessage contactUs(@RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "phoneNumber", defaultValue = "") String phoneNumber, @RequestParam(value = "email", defaultValue = "") String email, @RequestParam(value = "type", defaultValue = "") String type, HttpServletRequest request) {
        int results = customerService.insertCustomer(name, phoneNumber, email, type);
        if (results == 1) {
            if (email.length() != 0) {//如果用户填写了邮箱
                String message = "      您好 " + name + ",您的联系方式我们已收到,我们将尽快联系您。";
                try {
                    this.sendEmailToCustomer(email, message);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            return new ResponseMessage(ResponseMessage.SUCCESS, "成功接受");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "数据库错误");
    }

    @PostMapping("updateMessageReadStatus.do")
    public ResponseMessage updateMessageReadStatus(@PathParam("message_id") int message_id, HttpServletRequest request) {
        int results = messageService.updateMessageReadStatusupdate(message_id);
        if (results == 1) {
            return new ResponseMessage(ResponseMessage.SUCCESS, "消息已读");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "数据库错误");
    }

    @PostMapping("deleteMessageReadStatus.do")
    public ResponseMessage deleteMessageReadStatus(@PathParam("message_id") int message_id, HttpServletRequest request) {
        int results = messageService.deleteMessageReadStatusupdate(message_id);
        if (results == 1) {
            return new ResponseMessage(ResponseMessage.SUCCESS, "消息已删除");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "数据库错误");
    }

    @PostMapping("collectMeeting.do")
    public ResponseMessage collectMeeting(@PathParam("user_id") int user_id, @PathParam("meeting_id") int meeting_id, HttpServletRequest request) {
        timeJudgeController.updateMeetingAndPersonStatus(meeting_id);//更新会议状态
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String collection_time = df.format(new Date());
        String collectionId = meetingCollectionService.getCollectionIdByUserIdAndMeetingId(user_id, meeting_id);
        if (collectionId == null) {
            int results = meetingCollectionService.addCollectMeeting(user_id, meeting_id, collection_time);
            if (results == 1) {
                return new ResponseMessage(ResponseMessage.SUCCESS, "收藏成功");
            }
            return new ResponseMessage(ResponseMessage.ERROR, "数据库错误");
        } else {
            int collectionStatusResults = meetingCollectionService.setCollectionStatus(collectionId, 1, collection_time);
            if (collectionStatusResults == 1) {
                return new ResponseMessage(ResponseMessage.SUCCESS, "收藏成功", collection_time);
            }
            return new ResponseMessage(ResponseMessage.ERROR, "数据库错误");
        }
    }

    @PostMapping("outOfCollectMeeting.do")
    public ResponseMessage outOfCollectMeeting(@PathParam("user_id") int user_id, @PathParam("meeting_id") int meeting_id, HttpServletRequest request) {
        timeJudgeController.updateMeetingAndPersonStatus(meeting_id);//更新会议状态
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String collection_time = df.format(new Date());
        String collectionId = meetingCollectionService.getCollectionIdByUserIdAndMeetingId(user_id, meeting_id);
        if (collectionId == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "数据库错误,用户未曾收藏过此会议");
        } else {
            int collectionStatusResults = meetingCollectionService.setCollectionStatus(collectionId, 0, collection_time);
            if (collectionStatusResults == 1) {
                return new ResponseMessage(ResponseMessage.SUCCESS, "取消收藏成功");
            }
            return new ResponseMessage(ResponseMessage.ERROR, "数据库错误");
        }
    }

    /**
     * 在数据库里面插入消息（审核通过）
     *
     * @param user_id      会议预定者id
     * @param to_user_ids  会议受邀者id数组
     * @param meeting_name 会议名称
     * @param meeting_time 会议开始时间
     * @param timeDuring   会议持续时间
     */
    public Boolean sendMessage(int user_id, int[] to_user_ids, String meeting_name, String meeting_time, String timeDuring) {
        int department_id = departmentUserService.getDepartmentIdByUserId(user_id);
        int company_id = departmentService.getCompanyIdByDepartmentId(department_id);
        int AIManagerId = managerService.getAIManagerId(company_id);
        User userSelf = userService.getUserinfomationByUserid(user_id);//获得用户自己对象信息
        String content = "你好 " + userSelf.getUser_name() + " 你预定于 " + meeting_time + " 开始的 " + meeting_name + " 审核通过！请尽快准备参会材料";
        int addResults = messageService.addMessage(AIManagerId, user_id, content);//向会议预定者发送审核通过message
        for (int i = 0; i < to_user_ids.length; i++) {
            String[] Invitors = userService.getUserNamesByUserIds(to_user_ids);
            String contentToInvitor = "你好 " + Invitors[i] + " " + userSelf.getUser_name() + " 邀请您参与 " + meeting_name + " ，会议将于 " + meeting_time + " 开始！请尽快准备参会材料";
            messageService.addMessage(AIManagerId, to_user_ids[i], contentToInvitor);
        }
        if (addResults == 1) {
            try {
                Boolean sendResult = sendEmailToApplicant(user_id, meeting_time, timeDuring, meeting_name);//向会议预定者发送邮件
                if (sendResult) {
                    return sendEmailForInvitation(user_id, meeting_time, timeDuring, meeting_name, to_user_ids);//向会议受邀者发送邮件
                }
                return false;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @PostMapping("getMyMessage.json")
    public ResponseMessage getMyMessage(@PathParam("user_id") int user_id, HttpServletRequest request) {
        int department_id = departmentUserService.getDepartmentIdByUserId(user_id);
        int company_id = departmentService.getCompanyIdByDepartmentId(department_id);
        int[] managerIds = managerService.getManagerIdsByCompanyId(company_id);
        List<Map<String, Object>> Messages = messageService.getMessages(user_id, managerIds);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设定时间格式规范
        for (int i = 0; i < Messages.size(); i++) {//转换日期格式
            Messages.get(i).put("message_datetime", sdf.format(Messages.get(i).get("message_datetime")));
            if ((Integer) Messages.get(i).get("message_read_status") == 0) {//改变消息读状态
                Messages.get(i).put("message_read_status", "未读");
            } else if ((Integer) Messages.get(i).get("message_read_status") == 1) {
                Messages.get(i).put("message_read_status", "已读");
            }
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "用户得到的消息", Messages);
    }

    /**
     * 完善用户信息
     */
    @PostMapping("perfectingInformation.do")
    public ResponseMessage perfectingInformation(@PathParam("user_id") int user_id, @PathParam("phone_number") String phone_number, @PathParam("email") String email, @PathParam("security_question1") int security_question1, @PathParam("security_answer1") String security_answer1, @PathParam("security_question2") int security_question2, @PathParam("security_answer2") String security_answer2, @PathParam("security_question3") int security_question3, @PathParam("security_answer3") String security_answer3) {
        String addResult = userFaceInfoService.getAddFaceIdStatusByUserId(user_id);
        if (addResult != null) {//如果用户人脸信息已经完善
            int results = userService.perfectUserInfomation(user_id, phone_number, email, security_question1, security_answer1, security_question2, security_answer2, security_question3, security_answer3);
            if (results == 1) {
                return new ResponseMessage(ResponseMessage.SUCCESS, "用户完善信息成功");
            }
            return new ResponseMessage(ResponseMessage.ERROR, "用户完善信息失败");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "用户人脸信息未完善");
    }

    /**
     * 安卓端验证邮箱时使用
     *
     * @param mail_account 邮箱账户
     * @param randomCode   随机验证码
     */
    @PostMapping("sendEmail.do")
    public ResponseMessage sendEmailForLoginEmail(@PathParam("mail_account") String mail_account, @PathParam("randomCode") String randomCode) {
        String subject = "【一会儿】邮箱验证";
        String[] toUsers = new String[]{mail_account};
        String content = "      你好，这里是【一会儿】邮箱验证，你接收到的邮箱验证码是：" + randomCode + " ,请在10分钟内完成验证";
        boolean isSend = MailAssistant.sendEmail(subject, toUsers, null, content, null);
        if (isSend) {
            return new ResponseMessage(ResponseMessage.SUCCESS, "邮件发送成功");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "邮件发送失败");
    }

    /**
     * 会议审核通过后发送邀请
     *
     * @param user_id      会议申请人id
     * @param timeStart    会议开始时间
     * @param timeDuring   会议持续时间
     * @param meeting_name 会议名称
     * @param user_ids     会议受邀者Id数组
     */
    private Boolean sendEmailForInvitation(@PathParam("applicant") int user_id, @PathParam("timeStart") String timeStart, @PathParam("timeDuring") String timeDuring, @PathParam("meeting_name") String meeting_name, @PathParam("user_ids") int[] user_ids) throws JsonProcessingException {
        String subject = "【一会儿】会议邀请提醒";
        String[] toUsers = userService.getUserEmailsByUserIds(user_ids);
        User user = userService.getUserinfomationByUserid(user_id);
        String content = "\t<div style=\"margin: 0 auto ;width: 80%;padding-top: 20px\">\n" +
                "\t\t <p>你好！</p>\n" +
                "    <p style=\"text-indent:2em; font-size: 10px\">" +
                "<strong>"+user.getUser_name()+"</strong>" +
                "邀请你于" +
                "<strong>"+timeStart+"</strong>" +
                "参加&nbsp;" +
                "<em>" +meeting_name+ "</em>" +
                "&nbsp;,会议时长约为" +
                "<strong>"+timeDuring+"</strong>" +
                "分钟.</p>\n" +
                "\n" +
                "    <p style=\"text-indent:2em;font-size: 10px\">请尽快登陆【一会儿】小程序端/【一会儿】安卓/【一会儿】网页端查看<p>\n" +
                "\t</div>\n" +
                "   \n";
        //String content = "你好，" + user.getUser_name() + "邀请你于 " + timeStart + "参加 " + meeting_name + ",会议时长约为 " + timeDuring + " 分钟,请尽快登陆【一会儿】小程序端/【一会儿】安卓/【一会儿】网页端查看";
        return MailAssistant.sendEmail(subject, toUsers, null, content, null);

    }

    /**
     * 会议审核通过后发送邀请
     * @param user_id      会议申请人id
     * @param timeStart    会议开始时间
     * @param timeDuring   会议持续时间
     * @param meeting_name 会议名称
     */
    private Boolean sendEmailToApplicant(@PathParam("applicant") int user_id, @PathParam("timeStart") String timeStart, @PathParam("timeDuring") String timeDuring, @PathParam("meeting_name") String meeting_name) throws JsonProcessingException {
        String subject = "【一会儿】会议邀请提醒";
        User user = userService.getUserinfomationByUserid(user_id);
        String content = "你好，" + user.getUser_name() + "你申请的 " + meeting_name + " 已通过审核，会议将于" + timeStart + "开始,会议时长约为 " + timeDuring + " 分钟,请尽快登陆【一会儿】小程序端/【一会儿】安卓/【一会儿】网页端查看";
        return MailAssistant.sendEmail(subject, new String[]{user.getUser_email()}, null, content, null);
    }

    /**
     * 填写了联系方式的顾客得到邮件提醒
     *
     * @param email   会议开始时间
     * @param content 会议持续时间
     */
    private Boolean sendEmailToCustomer(@PathParam("email") String email, @PathParam("content") String content) throws JsonProcessingException {
        String subject = "【一会儿】";
        String[] toUesr = new String[]{email};
        return MailAssistant.sendEmail(subject, toUesr, null, content, null);
    }

    /**
     * 得到用户状态（是否未完善信息）
     */
    @PostMapping("userStatus.json")
    public ResponseMessage getUserStatus(@PathParam("user_id") int user_id) {
        int user_status = userService.getUserStatusByUserid(user_id);
        return new ResponseMessage(ResponseMessage.SUCCESS, "用户得到的消息", user_status);
    }

    /**
     * 小程序用户扫描二维码
     */
    @PostMapping("QRCodeInfomation.json")
    public ResponseMessage QRCodeInfomation(@PathParam("qrcode") String qrcode, @PathParam("user_id") int user_id) {
        int department_id = departmentUserService.getDepartmentIdByUserId(user_id);
        int company_id = departmentService.getCompanyIdByDepartmentId(department_id);
        String[] qrcodeCutting = qrcode.split(",");
        int id = Integer.parseInt(qrcodeCutting[1]);
        if (qrcodeCutting[0].equals("0")) {//会议
            int location = meetingService.getLocationByMeetingId(id);
            String judge = roomService.judgeRoomBelongsCompany(location, company_id);
            if (judge != null) {
                return meetingRoomObj.getSearchDetail(id, "2");
            }
            return new ResponseMessage(ResponseMessage.ERROR, "会议不属于本公司，无法查看");
        } else if (qrcodeCutting[0].equals("1")) {//会议室
            String judge = roomService.judgeRoomBelongsCompany(id, company_id);
            if (judge != null) {
                return meetingRoomObj.getSearchDetail(id, "1");
            }
            return new ResponseMessage(ResponseMessage.ERROR, "会议室不属于本公司，无法查看");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "二维码格式错误无法识别");
    }


}
